const utils = require("../utils/utils");
const BuildMoudle = require("../module/BuildModule");
const path = require("path");
const walk = require("../utils/walk");
const Define = require("../define");
const dirutils = require("../utils/dirutils");

class ChatBuilder {
    /**
     * @type {BuildMoudle}
     */
    module;

    dir;

    info;

    temp;

    old;

    /**
     *
     * @param {string} dir
     * @param {*} info
     * @param {BuildMoudle} mod
     * @returns
     */
    build(dir, info, mod) {
        this.dir = dir;
        this.info = info;
        this.module = mod;
        this.temp = utils.readObj(this.module.tempPath);

        const oldid = this.info.OldGameID;
        this.old = walk.dirWalkFiles(
            mod.inputPath,
            dirutils.getCfgPaths(oldid)
        );

        let chat = [];
        let result = {
            "": chat,
        };
        const ids = this.old.chat;
        for (let i = 0; i < ids.length; i++) {
            chat.push(this.createItem(i, ids[i]));
        }

        this.write(result);
    }

    createItem(i, id) {
        let text = this.temp[id].text;
        return {
            index: i + 1,
            SpeakText: text,
            SpeakFileName: `chat_${id}`,
        };
    }

    write(result) {
        const name = path.basename(this.dir);
        let file = path.join(
            this.module.outputPath,
            name,
            Define.DIR_CONFIG,
            Define.FILE_SPEAK
        );
        utils.writeFile(file, result, false, true);
    }
}

module.exports = ChatBuilder;
